package com.yanqu.road.server.cmd.union.activitygroup;

import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.logic.pb.UnionActivityGroupPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.CrossBattleServer;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.suitang.CrossSuiTangMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.union.activitygroup.UnionActivityGroupUpdateUnionLogic;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = CrossProtocol.C2_UNION_ACTIVITY_GROUP_SYNC, desc = "同步分组数据")
public class CrossUnionActivityGroupSyncCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        UnionActivityGroupProto.UnionActivityGroupSyncReqMsg syncMsg = UnionActivityGroupProto.UnionActivityGroupSyncReqMsg.parseFrom(packet.getBytes());

        // 【全量数据处理】
        if (syncMsg.getAllGroupCount() > 0) {
            Map<Integer, Map<Integer, UnionActivityGroup>> mapMap = new ConcurrentHashMap<>();
            for (UnionActivityGroupProto.UnionActivityGroupMsg groupMsg : syncMsg.getAllGroupList()) {
                if (groupMsg.getCrossServerId() != CrossBattleServer.getInstance().getServerId()) {
                    continue; // 去掉不需要的数据
                }
                UnionActivityGroup group = UnionActivityGroupPb.parseUnionActivityGroup(groupMsg);
                mapMap.putIfAbsent(group.getActivityId(), new ConcurrentHashMap<>());
                mapMap.get(group.getActivityId()).put(group.getGroupId(), group);
            }

            for (Map.Entry<Integer, Map<Integer, UnionActivityGroup>> entry : mapMap.entrySet()) {
                int activityId = entry.getKey();
                Map<Integer, UnionActivityGroup> groupMap = entry.getValue();
                Cross2UnionActivityGroupMgr.putGroupMap(activityId, groupMap);

                ActivityMgr.getActivityInfoMayLoadDB(activityId);//同步时把基础活动数据加载到内存

                // 【原路返回确认】
                UnionActivityGroupProto.UnionActivityGroupSyncRespMsg.Builder builder = UnionActivityGroupProto.UnionActivityGroupSyncRespMsg.newBuilder();
                builder.setActivityId(activityId);
                builder.setCrossId(CrossBattleServer.getInstance().getServerId());
                YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_UNION_ACTIVITY_GROUP_SYNC_RESP, builder);
                MessageHelper.sendPacket(serverId, 0, message);

                // 隋唐分组完成，进行初始化
                CrossSuiTangMgr.loadActivity(activityId);
            }
        }

        // 【增量数据处理】
        if (syncMsg.getUpdateUnionCount() > 0) {
            for (UnionActivityGroupProto.UnionActivityGroupUnionUpdateMsg unionUpdateMsg : syncMsg.getUpdateUnionList()) {
                UnionActivityGroupUnion updateUnion = UnionActivityGroupPb.parseUnionActivityGroupUnion(unionUpdateMsg);
                Cross2UnionActivityGroupMgr.updateGroupUnion(updateUnion);
                UnionActivityGroupUpdateUnionLogic.onUnionUpdate(updateUnion);
            }
        }

    }

    @Override
    public void sendData() {
    }
}
